package dao;

import entity.Chat;
import entity.User;
import tools.DBPoolHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ChatDao {

    private String sql = "";
    private PreparedStatement pstmt;
    private ResultSet rs;
    private Connection conn = null;

    private ArrayList<Chat> chats = new ArrayList<Chat>();
    private Chat chat;

    public Connection getConn() {
        try {
            if ((conn == null) || conn.isClosed()) {
                conn = DBPoolHelper.getConnection();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    public void close() {
        try {
            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (conn != null)
                DBPoolHelper.close(conn);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            rs = null;
            pstmt = null;
            conn = null;
        }
    }

    public ArrayList<Chat> getByRs(ResultSet rs) {
        try {
            if (rs == null || !rs.next()) {
                return null;
            }
            chats.clear();
            do {
                chat = new Chat();
                chat.setSenderQQ(rs.getInt("csenderqq"));
                chat.setReceiveQQ(rs.getInt("creceiveqq"));
                chat.setSendTime(rs.getLong("cdate"));
                chat.setContent(rs.getString("content"));
                chats.add(chat);
            } while (rs.next());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return chats;
    }

    /**
     * 添加单条消息
     *
     * @param chat
     * @return
     */
    public boolean add(Chat chat) {
        sql = "insert into chatInfo(csenderqq,creceiveqq,cdate,content) values(?,?,?,?)";
        int result = 0;
        try {
            pstmt = getConn().prepareStatement(sql);
            pstmt.setInt(1, chat.getSenderQQ());
            pstmt.setInt(2, chat.getReceiveQQ());
            pstmt.setLong(3, chat.getSendTime());
            pstmt.setString(4, chat.getContent());
            result = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    public ArrayList<Chat> getChatsByQQ(int qq, int fqq) {
        getConn();
        sql = "select * from chatInfo where (csenderqq = ? or csenderqq = ?) and (creceiveqq = ? or creceiveqq = ?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, qq);
            pstmt.setInt(2, fqq);
            pstmt.setInt(3, qq);
            pstmt.setInt(4, fqq);
            rs = pstmt.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return getByRs(rs);
    }

}

